from DrissionPage import ChromiumPage

class CmsAutoMation:
    def __init__(self,page:ChromiumPage)-> None:
        self.page = page
        
    def start(self,url,username,password):       
        # 跳转到登录页面
        self.page.get(url)
        # 定位到账号文本框，获取文本框元素，输入对文本框输入账号
        self.page.ele('#username').input(username)
        # 定位到密码文本框并输入密码
        self.page.ele('#password').input(password)
        self.page.wait(0.2)
        # 点击登录按钮
        self.page.ele('xpath=//*[@id="doLogin"]/button').click()

    def article_manage(self):
        self.page.wait(0.5)
        # 进入内容管理
        self.page.ele('xpath=//*[@id="main-container"]/div[1]/div[3]/ul/li[2]/a').click()
        self.page.wait(0.5)
        # 道路救援案例
        # self.page.ele('xpath=//*[@id="main-container"]/div[5]/div[1]/ul/li[8]/a').click()
        # 道路救援资讯
        self.page.ele('xpath=//*[@id="main-container"]/div[5]/div[1]/ul/li[9]/a').click()

    def article_copy(self,webseo):
        # 选择要复制的文章并复制
        self.page.wait.ele_displayed('xpath=//*[@id="article_list"]/tr[1]/td[1]/div')
        self.page.ele('xpath=//*[@id="article_list"]/tr[1]/td[1]/div').click('js')
        # 点击复制
        self.page.ele('xpath=//*[@id="main-container"]/div[5]/div[2]/div[3]/button').click()
        self.page.wait(0.5)
        self.page.wait.ele_displayed('xpath=//*[@id="main-container"]/div[5]/div[2]/div[3]/ul/li[3]/a')
        self.page.ele('xpath=//*[@id="main-container"]/div[5]/div[2]/div[3]/ul/li[3]/a').click('js')
        # 点击确定
        self.page.wait(0.5)
        self.page.wait.ele_displayed('xpath=//*[@id="layui-layer1"]/div[3]/a[1]')
        self.page.ele('xpath=//*[@id="layui-layer1"]/div[3]/a[1]').click('js')
        self.page.wait(0.5)
        self.page.wait.ele_displayed('xpath=//*[@id="layui-layer2"]/div[3]/a')
        self.page.ele('xpath=//*[@id="layui-layer2"]/div[3]/a').click('js')
        #编辑复制的文章
        self.page.wait(1)
        self.page.ele('xpath=//*[@id="article_list"]/tr[1]/td[9]/a[2]').click()

        # 清空基本信息
        self.page.wait(0.5)
        print('进入发布页！')
        # 标题
        self.page.ele('#tiqutitle').clear()
        # 封面图片
        self.page.ele('#path1').clear()
        # 排序
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[1]/div[3]/div/input').clear()
        # 副标题
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[1]/div[4]/div/input').clear()
        # 发布人
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[1]/div[5]/div/input').clear()

        # 填充基本信息
        self.page.wait(0.2)
        # 标题
        self.page.ele('#tiqutitle').input(webseo['title'])
        # 封面图片
        self.page.ele('#path1').input(webseo['litpic'])
        # 排序
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[1]/div[3]/div/input').input(webseo['id'])
        # 副标题
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[1]/div[4]/div/input').input(webseo['title'])
        # 发布人
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[1]/div[5]/div/input').input('全国24小时道路救援')

        # 清空SEO设置
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/ul/li[2]').click()
        self.page.wait(0.2)
        # 自定义页面title
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[2]/div[1]/div/input').clear()
        # 关键词
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[2]/div[2]/div/input').clear()
        # TAG标签
        self.page.ele('#tag').clear()

        # 填充SEO设置
        self.page.wait(0.2)
        # 自定义页面title
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[2]/div[1]/div/input').input('[prov_or_city]'+webseo['title'])
        # 关键词
        self.page.ele('xpath=//*[@id="formrec"]/div[3]/div/div[2]/div[2]/div/input').input(webseo['keywords'])
        # TAG标签
        self.page.ele('#tag').input(webseo['keywords'])
        # 提交
        self.page.ele('xpath=//*[@id="main-container"]/div[3]/div[2]/div[2]/div/button').click()

        #返回列表
        self.page.wait(0.5)
        print('第{}篇发布完毕！'.format(webseo['id']))
        self.page.ele('xpath=//*[@id="layui-layer1"]/div[3]/a').click()
        self.page.wait(0.5)

